package com.limeng.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.limeng.domain.DAOContact;
import com.limeng.exceptions.DAOException;
import com.limeng.hibernate.ModelUtil;
import com.limeng.model.Contact;

public class DAOContactImpl extends DAOContact {

	@Override
	public Contact loadContact(Long id) throws DAOException {
		Contact contact = null;
		try {
			ModelUtil.getSession().beginTransaction();
			contact = (Contact) ModelUtil.getSession().get(Contact.class, id);
			ModelUtil.getSession().getTransaction().commit();
		} catch (Exception e) {
			if (id != null) {
				throw new DAOException("Could not retrieve objects with id  "
						+ id + ".  " + e.getMessage());
			} else {
				throw new DAOException(
						"Could not retrieve objects with null id.  "
								+ e.getMessage());
			}
		}
		if (contact == null) {
			if (id != null) {
				throw new DAOException("Could not find object with id  " + id
						+ ".  ");
			} else {
				throw new DAOException("Could not find object with null id ");
			}
		}
		return contact;
	}

	@Override
	public Contact saveContact(Contact contact) throws DAOException {

		try {
			ModelUtil.getSession().beginTransaction();
			// contact.getAddress().getContacts().add(contact);
			if (contact.getContactID() != null) {
				ModelUtil.getSession().update(contact);
			} else {
				ModelUtil.getSession().save(contact);
			}
			ModelUtil.getSession().getTransaction().commit();
			return contact;
		} catch (Exception e) {
			if (contact == null) {
				throw new DAOException(
						"Could not retrieve objects for null criteria " + ".  "
								+ e.getMessage());
			} else {
				throw new DAOException(
						"Could not retrieve objects for criteria : "
								+ contact.toString() + ".  " + e.getMessage());
			}
		}

	}

	@Override
	public String updateContact(Contact contact) {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Contact> getAllContact() {
		ModelUtil.getSession().beginTransaction();
		List<Contact> contactList = new ArrayList<Contact>();
		contactList = ModelUtil.getSession().createCriteria(Contact.class)
				.list();
		ModelUtil.getSession().getTransaction().commit();
		return contactList;

	}

	@Override
	public HashMap<String, String> deleteContactList(List<Contact> contactList) {
		HashMap<String, String> deleteMap = new HashMap<String, String>();
		for (Contact contact : contactList) {

			ModelUtil.getSession().beginTransaction();
			if (contact.getContactID() != null) {
				ModelUtil.getSession().delete(contact);
				deleteMap.put(contact.getContactID().toString(),
						"has been deleted");
			} else {
				deleteMap.put("error", "contact ID can not be null");
			}
			ModelUtil.getSession().getTransaction().commit();

		}
		return deleteMap;
	}

	@Override
	public String deleteContact(Contact contact) throws DAOException {

		if (contact.getContactID() != null) {
			contact=this.loadContact(contact.getContactID());
			ModelUtil.getSession().beginTransaction();
			ModelUtil.getSession().delete(contact);
			ModelUtil.getSession().getTransaction().commit();
			return ("deleted successful");
		} else {
			return ("error, contact ID can not be null");
		}
	}

}
